<!DOCTYPE html>
<html>
<head>
	<div th:include="base-libs-easyui :: base-libs-easyui" />
<title>机构病区信息</title>
<style type="text/css">
	body {
		padding: 0px 10px;
	}
	.form-table input{
		width: 500px;
		padding-left: 4px;
	}
	.form-table select{
		width: 504px!important;
	}
	.tree-icon{
		display: none;
	}

	.bt-selected{
		background-color: #D0E9C6;
	}
</style>
</head>
<script th:inline="javascript">
    /**读取request中的初始化数据*/
    var requestData = [[${requestData}]];
</script>
<script type="text/javascript">
	$(function() {
		initView();
        initData();
	});

	function initData() {
		var updatedBqxx = $("#formBqxx").data("updatedBqxx");
        if(updatedBqxx)
        {
			/*修改禁用医疗机构下拉框*/
            $("#combogridYljg").combogrid("readonly", true);
            /*修改禁用科室代码*/
            $("#KSDM").attr("disabled", "disabled");
            /*禁用病区代码*/
            $("#BQDM").attr("readonly", "readonly");
            queryKsxx(updatedBqxx["PTJGID"], setData);
        }else if(!requestData["isManager"]){
			var PTJGID = requestData["PTJGID"];
			var YLJGMC = requestData["YLJGMC"];
			var value = {PTJGID: PTJGID, YLJGMC: YLJGMC};
			$("#combogridYljg").combogrid("grid").datagrid("loadData", [value]);
			$("#combogridYljg").combogrid("setValue", value);
		}
	}

	function setData(){
	    var updatedBqxx = $("#formBqxx").data("updatedBqxx");
        $(".form-control").each(function(){
            var name = $(this).attr("name");
            if(!StringUtils.isEmpty(name)){
                var value = updatedBqxx[name];
                $(this).QuickField("setData", value);
			}
        });
		/*医疗机构赋值*/
		$("#combogridYljg").combogrid("setValue", {PTJGID: updatedBqxx["PTJGID"], YLJGMC: updatedBqxx["YLJGMC"]});
	}

	function initView() {
		$("#formBqxx").attr("action", contextRoot + "/bqxx/saveBqxx");
		$("#btnSave").on("click", btnSaveHandler);
        $("#btnClose").on("click", btnCloseHandler);
        var updatedBqxx = StoreCache.removeCache("curr_bqxx");
        var url = "";
        var readonly = !requestData["isManager"];
        if(updatedBqxx){
            $("#formBqxx").data("updatedBqxx", updatedBqxx);
            readonly = true;
		}else if(!readonly){
            url = contextRoot + "/yljgxx/queryPageSummary.service";
        }

        $("#combogridYljg").combogrid({
			panelHeight: 200
            ,height: 30
            ,singleSelect: true
            ,pagination: true
            ,idField: 'PTJGID'
            ,textField: 'YLJGMC'
            ,mode: "remote"
            ,url: url
			,readonly: readonly
            ,hasDownArrow: !readonly
			,delay: 1000
            ,reversed: true
            ,columns:[[
                {
                    field:'YLJGMC'
                    ,title:'机构名称'
                    ,width:240
                }
                ,{
                    field:'XZQHMC'
                    ,title:'行政区划'
                    ,width:80
                }
                ,{
                    field:'WSJGLBMC'
                    ,title:'机构类别'
                    ,width:80
                }
                ,{
                    field:'YYDJMC'
                    ,title:'机构等级'
                    ,width:80
                }
            ]]
            ,onBeforeLoad: function(queryParams) {
                var url = $(this).datagrid("options")["url"];
				/*URL不等空时再发送请求*/
                if (!StringUtils.isEmpty(url)) {
                    queryParams["pageSize"] = queryParams["rows"];
                    queryParams["pageNumber"] = queryParams["page"];
                    queryParams["YLJGMC"] = queryParams["q"];
                    return true;
                }else{
                    return false;
                }
            }
			,onChange: function(newValue, oldValue){
				/*新增响应改变事件*/
                if(!$("#formBqxx").data("updatedBqxx")){
					/*响应改变事件*/
                    var value = $("#combogridYljg").combogrid("grid").datagrid("getSelected");
                    if(value){
                        $("#PTJGID").val(value["PTJGID"]);
                        queryKsxx(value["PTJGID"]);
                    }else{
                        $("#PTJGID").val("");
                    }
                }
			}
        });
	}

    /**查询科室信息*/
    function queryKsxx(PTJGID, callback){
        $.QuickRemote.AjaxJson(contextRoot + "/ksxx/", queryKsxxHandler, {PTJGID: PTJGID, JB: "1"}, {callback: callback});
    }
    function queryKsxxHandler(result, params){
        $("#KSDM").createOptions(result, {needNull: "true", textField: "KSMC", valueField: "KSDM"});
        if(params && params["callback"]){
            params["callback"]();
        }
    }

	function btnSaveHandler(){
        if($(".form-control").QuickValidate())
        {
            var method = "add";
            var old = $("#formBqxx").data("updatedBqxx");
            if(old){
                $("#KSDM").removeAttr("disabled");
                method = "update";
			}
            var params = {method: method};
            $.QuickRemote.AjaxFiledFormSubmit("formBqxx", saveHandler, params);
        }
	}
	function saveHandler(result){
        var state = result["state"];
        if(state)
        {
            $.QuickAlert.alertNormal("保存成功", {callback: function(){
                $("#btnClose").trigger("click");
            }});
        }
        else
        {
            var info = result["info"];
            $.QuickAlert.alertFail({content: info["faultInfo"]});
        }
	}

	/**验证机构*/
	function validateJg(){
	    var PTJGID = $("input[name='PTJGID']").val();
        if(StringUtils.isEmpty(PTJGID)){
            return "请选择机构";
		}else{
            return "";
		}
	}

	/**返回按钮*/
	function btnCloseHandler() {
        parent.layer.closeAll();
	}
</script>

<body class="easyui-layout">
	<div region="center" style="border: none;">
		<form id="formBqxx">
			<table class="form-table">
				<tr>
					<td class="td-text">
						<label>机构名称</label>
					</td>
					<td class="td-content">
						<input id="combogridYljg" class="form-control" name="PTJGID"
							   quick-config="label:'机构名称', validateProxy:'validateJg', length: '64'">
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>科室名称</label>
					</td>
					<td class="td-content">
						<select id="KSDM" name="KSDM" class="form-control"
								quick-config="label:'科室名称', validateProxy:'notNull length', length: '64'"></select>
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>病区代码</label>
					</td>
					<td class="td-content">
						<input id="BQDM" name="BQDM" type="text" placeholder="病区代码"
							   quick-config="label:'病区代码', validateProxy:'notNull length', length: '64'"
							   class="form-control">
					</td>
				</tr>
				<tr>
					<td class="td-text">
						<label>病区名称</label>
					</td>
					<td class="td-content">
						<input id="BQMC" name="BQMC" type="text" placeholder="病区名称"
							   quick-config="label:'病区名称', validateProxy:'notNull length', length: '64'"
							   class="form-control">
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div region="south" style="height: 41px; padding-top: 6px;">
		<button id="btnClose" type="button" style="margin-right: 10px; float: right;">关闭</button>
		<button id="btnSave" type="button" style="margin-right: 10px; float: right;">保存</button>
	</div>
</body>
</html>


